package com.wang.mall.controller;

import com.wang.mall.bean.Product;
import com.wang.mall.bean.SmsSeckillSkuRelation;
import com.wang.mall.model.R;
import com.wang.mall.service.ProductService;
import com.wang.mall.service.SmsSeckillSessionService;
import com.wang.mall.service.SmsSeckillSkuRelationService;
import com.wang.mall.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("/skillProduct")
public class SkillProductController {

    @Autowired
    SmsSeckillSkuRelationService smsSeckillSkuRelationService;

    @Autowired
    SmsSeckillSessionService smsSeckillSessionService;

    @Autowired
    ProductService productService;

    @GetMapping
    public R<PageUtils> queryPage(@RequestParam Map<String, Object> params) {
        PageUtils page = smsSeckillSkuRelationService.queryPage(params);

        return R.ok(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("coupon:seckillskurelation:info")
    public R info(@PathVariable("id") Long id) {
        SmsSeckillSkuRelation seckillSkuRelation = smsSeckillSkuRelationService.getById(id);

        return R.ok(seckillSkuRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SmsSeckillSkuRelation seckillSkuRelation) {



        // 商品参与秒杀那么就不能再购物页面显示
        Long skuId = seckillSkuRelation.getSkuId();
        Product product = new Product();
        product.setStatus(0);
        product.setId(Math.toIntExact(skuId));
        productService.updateById(product);



        smsSeckillSkuRelationService.save(seckillSkuRelation);
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SmsSeckillSkuRelation seckillSkuRelation) {
        smsSeckillSkuRelationService.updateById(seckillSkuRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        smsSeckillSkuRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
